package com.wedu.modules.dss.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.Query;
import com.wedu.modules.dss.dao.DeviceInformationDao;
import com.wedu.modules.dss.entity.DeviceInformationEntity;
import com.wedu.modules.dss.entity.MaintenanceRecordsEntity;
import com.wedu.modules.dss.service.DeviceInformationService;
import com.wedu.modules.dss.service.MaintenanceRecordsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("DeviceInformationService")
public class DeviceInformationServiceImpl extends ServiceImpl<DeviceInformationDao, DeviceInformationEntity> implements DeviceInformationService {

    @Autowired
    private DeviceInformationDao deviceInformationDao;

    @Autowired
    private MaintenanceRecordsService maintenanceRecordsService;

    @Override
    public List<DeviceInformationEntity> getdeviceName() {

        Wrapper<DeviceInformationEntity> wrapper = new QueryWrapper<DeviceInformationEntity>()
                .eq("is_deleted",0);
        List<DeviceInformationEntity> deviceInformationEntityList = baseMapper.selectList(wrapper);
        return deviceInformationEntityList;
    }

    @Override
    public PageUtils queryPageWarning(Map<String, Object> params) {
        String deviceName = (String) params.get("deviceName");

        String maintenancePlan = (String) params.get("maintenancePlan");
        Date date = new Date();
        //先获取所有信息，再获取其预警时间，根据当前时间比较修改状态
        Wrapper<DeviceInformationEntity> wrapper = new QueryWrapper<>();
        List<DeviceInformationEntity> deviceList = deviceInformationDao.selectList(wrapper);
        for (DeviceInformationEntity deviceInformationEntity : deviceList) {
            if (deviceInformationEntity.getMaintenanceStatus() == 1){
                if (date.after(deviceInformationEntity.getNoticeTime())){
                    deviceInformationEntity.setMaintenanceStatus(-1);
                    deviceInformationDao.updateById(deviceInformationEntity);
                }
            }
        }

        IPage<DeviceInformationEntity> page = this.page(
                new Query<DeviceInformationEntity>().getPage(params),
                new QueryWrapper<DeviceInformationEntity>()
                .like(StringUtils.isNotBlank(deviceName),"device_name",deviceName)
                .eq(StringUtils.isNotBlank(maintenancePlan),"maintenance_plan",maintenancePlan)
                .eq("maintenance_status",-1)
                .eq("is_deleted",0)
                .orderByDesc("create_time")
        );
        return new PageUtils(page);
    }

    @Override
    public void maintenance(Long[] ids,Long id) {
        for (Long aLong : ids) {
            QueryWrapper<DeviceInformationEntity> wrapper = new QueryWrapper<DeviceInformationEntity>()
                    .eq("id",aLong);
            DeviceInformationEntity deviceInformationEntity = deviceInformationDao.selectOne(wrapper);
            deviceInformationEntity.setMaintenanceStatus(0);
            MaintenanceRecordsEntity maintenanceRecordsEntity = new MaintenanceRecordsEntity();
            maintenanceRecordsEntity.setId(aLong);
            Date date = new Date();
            //添加在记录表中
            maintenanceRecordsEntity.setCreateBy(id);
            maintenanceRecordsEntity.setCreateTime(date);
            maintenanceRecordsEntity.setMaintenanceTime(date);
            maintenanceRecordsEntity.setUserId(id);
            maintenanceRecordsEntity.setDeviceId(deviceInformationEntity.getDeviceId());
            maintenanceRecordsService.save(maintenanceRecordsEntity);
            if (deviceInformationEntity.getMaintenancePlan().equals("1")){
                Integer x = deviceInformationEntity.getMaintenanceCycle();
                Integer y = deviceInformationEntity.getAdvanceTime();
                Date date1 = new Date();
                Calendar calendar1 = Calendar.getInstance();
                calendar1.add(Calendar.DATE,x);
                date1 = calendar1.getTime();
                deviceInformationEntity.setMaintenanceDueTime(date1);

                Date date2 = new Date();
                Calendar calendar2 = Calendar.getInstance();
                calendar2.add(Calendar.DATE,(x-y));
                date2 = calendar2.getTime();
                deviceInformationEntity.setNoticeTime(date2);
            }else if (deviceInformationEntity.getMaintenancePlan().equals("2")){
                Integer x = deviceInformationEntity.getMaintenanceCycle();
                Date date1 = new Date();
                Calendar calendar1 = Calendar.getInstance();
                calendar1.add(Calendar.DATE,x);
                date1 = calendar1.getTime();
                deviceInformationEntity.setMaintenanceDueTime(date1);
                deviceInformationEntity.setNoticeTime(date1);
            }
            deviceInformationDao.updateById(deviceInformationEntity);//塞实体类

        }
    }
}
